package com.qyhl.webtv.commonlib.item;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.R;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.news.GlobalNewsBean;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.Map;

/**
 * Created by qyhl on 2017/11/15.
 * 列表无图新闻类型
 */

public class ItemNoPicture implements ItemViewDelegate<GlobalNewsBean> {
    private Context context;

    public ItemNoPicture(Context context) {
        this.context = context;
    }
    @Override
    public int getItemViewLayoutId() {
        return R.layout.news_item_newslist_nopicture;
    }

    @Override
    public boolean isForViewType(GlobalNewsBean item, int position) {
        return !item.isHasPicture() && !"0".equals(item.getType()) && !"15".equals(item.getType()) && !"16".equals(item.getType()) && !"17".equals(item.getType()) && !"18".equals(item.getType());

    }

    @Override
    public void convert(ViewHolder holder, GlobalNewsBean bean, int position) {
        //标题
        TextView title = (TextView) holder.getView(R.id.newslist_nopic_title);
        if (StringUtils.isNotEmpty(bean.getTitle())) {
            title.setVisibility(View.VISIBLE);
            title.setText(bean.getTitle());
        } else {
            title.setVisibility(View.GONE);
        }

        title.setTextColor(context.getResources().getColor(R.color.global_black_lv1));
        if (CommonUtils.getInstance().getHasHistory()) {
            if (Hawk.contains("readHistory")) {
                Map<String, String> historyMap = CommonUtils.getInstance().getReadHistory();
                for (Map.Entry<String, String> vo : historyMap.entrySet()) {
                    String newsId = (String) vo.getKey();
                    if (bean.getNewsId().equals(newsId)) {
                        title.setTextColor(context.getResources().getColor(R.color.global_gray_lv1));
                        break;
                    }
                }
            }
        }


        //浏览数
        TextView scan = (TextView) holder.getView(R.id.newslist_nopic_scan);
        try {
            int minPlay = Integer.parseInt(CommonUtils.getInstance().getMinPlay());
            if (minPlay == 0) {
                scan.setVisibility(View.GONE);
            } else {
                //判断评论数是否大于最少评论数
                if (StringUtils.isEmpty(bean.getScanNum() + "")) {
                    scan.setVisibility(View.GONE);
                } else if (bean.getScanNum() >=
                        minPlay) {
                    scan.setText(StringUtils.transformNum(bean.getScanNum() + "") + "阅");
                    scan.setVisibility(View.VISIBLE);
                } else {
                    scan.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
            scan.setVisibility(View.GONE);
        }

        //来源
        TextView mOrigin = (TextView) holder.getView(R.id.newslist_nopic_gather);
        if (StringUtils.isNotEmpty(bean.getOrigin())) {
            mOrigin.setVisibility(View.VISIBLE);
            mOrigin.setText(bean.getOrigin());
        } else {
            mOrigin.setVisibility(View.GONE);
        }

        //评论数
        TextView comment = (TextView) holder.getView(R.id.newslist_nopic_comment);
        try {
            int commentNum = Integer.parseInt(CommonUtils.getInstance().getCommentNum());
            if (commentNum == 0) {
                comment.setVisibility(View.GONE);
            } else {
                //判断评论数是否大于最少浏览数
                if (StringUtils.isEmpty(bean.getCommentNum() + "")) {
                    comment.setVisibility(View.GONE);
                } else if (bean.getCommentNum() >=
                        commentNum) {
                    comment.setText(StringUtils.transformNum(bean.getCommentNum() + "") + "评");
                    comment.setVisibility(View.VISIBLE);
                } else {
                    comment.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
            comment.setVisibility(View.GONE);
        }

    }
}
